/*
Sanctions, Aid, and Voting Patterns in the United Nations General Assembly
commands for creating tables and figures from main text
*/

**# Load Data and Put Data Editor Files in Working Direcotry
	/* insert your working directory here */
	use "SUNV-data.dta", clear
	version 16
	
	/*
	There are four graph editor files included with the data archive. These should
	be placed in the active working directory so that this do file can access them.
	The files are named:
	Fig1A.grec
	Fig1B.grec
	Fig2A.grec
	Fig2B.grec
	*/	
	
**# Make Global Controls
	global cvarl = "lUCDPInterConf lUCDPIntraConf latopdefense2 lVDEMv2x_polyarchy2 lunderIMF2 llnWDIGDPPCCUS2 llnWDIMilExPGDP2 llnCOWTTradeWUSPGDPWDI" 
		
**# Table 1: 
	* OLS (Columns 1 and 2)
		est clear
		*Table 1: Column 1
		xtreg absidealimportantdiff lEUSimpositionUS zllnGBAidTotPGDPWDIa lEUSThOnly ///
		$cvarl i.year902, fe cluster(ccode2) 
		eststo m1
		vif, uncentered
		estat ic
			
		*Table 1: Column 2
		xtreg absidealimportantdiff c.lEUSimpositionUS##c.zllnGBAidTotPGDPWDIa lEUSThOnly ///
		$cvarl i.year902, fe cluster(ccode2)
		eststo m2
		vif, uncentered
		estat ic
	
	* Make Table 1: (Columns 1 and 2)	
		esttab, bic replace onecell nogap obslast ///
		keep(lEUSimpositionUS zllnGBAidTotPGDPWDIa c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl) ///
		order(lEUSimpositionUS zllnGBAidTotPGDPWDIa c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl) ///
		star(* .1 ** .05 *** .01) label  b(2) t(2) 
		
	*Table 1: Column 2: Reverse Interaction for Interpretation
		xtreg absidealimportantdiff c.lNonEUSimpositionUS##c.zllnGBAidTotPGDPWDIa lEUSThOnly ///
		$cvarl i.year902, fe cluster(ccode2)
  
	* 2SLS Model (Columns 3 and 4)
		est clear
		ivreg2 absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl ///
		(lEUSimpositionUS = llEUSimpositionUS lEUSUSTot) i.ccode2 i.year902 ///
		,  first savefprefix(first) redundant(llEUSimpositionUS lEUSUSTot) ///
		endog(lEUSimpositionUS) savefirst cluster(ccode2) partial(i.ccode2 i.year902) 
		eststo 
		vif, uncentered
		estat ic
							
		* Make Table 1: Column 4: Second Stage Results
			esttab, bic replace onecell nogap obslast star(* .1 ** .05 *** .01) label b(2) t(2) 
	
		* Make Table 1: Coumn 3: first stage results	
			esttab first*, ///
			keep(zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl lEUSUSTot) ///
			order(zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl lEUSUSTot) ///
			bic replace onecell nogap obslast star(* .1 ** .05 *** .01) label  b(2) t(2) 
	
	* 2SLS Interaction Model (Columns 5, 6, 7)
		est clear
		ivreg2 absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl  ///
		(lEUSimpositionUS zlImpUSXllnAidPGDPa = llEUSimpositionUS l.zlImpUSXllnAidPGDPa ///
		lEUSUSTot zlEUSUSTotXllnAidPGDPa) i.ccode2 i.year902 , first savefprefix(first) ///
		redundant(llEUSimpositionUS l.zlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) /// 
		endog(lEUSimpositionUS zlImpUSXllnAidPGDPa) savefirst cluster(ccode2) partial(i.ccode2 i.year902)
		eststo
		vif, uncentered
		estat ic
		
		* Make Table 1: Column 7: Second Stage Results
			esttab, bic replace onecell nogap obslast ///
			order(lEUSimpositionUS zllnGBAidTotPGDPWDIa zlImpUSXllnAidPGDPa lEUSThOnly $cvarl) ///
			star(* .1 ** .05 *** .01) label  b(2) t(2) 
		
		* Make Table 1: Columns 5 and 6: first stage results	
			esttab first*, ///
			keep(zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl lEUSUSTot zlEUSUSTotXllnAidPGDPa) ///
			order(zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl lEUSUSTot zlEUSUSTotXllnAidPGDPa) ///
			bic replace onecell nogap obslast ///
			star(* .1 ** .05 *** .01) label  b(2) t(2) 
			
	* Table 1: Column 7: Reverse Interaction for Interpretation 
		ivregress 2sls absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl ///
		(lNonEUSimpositionUS c.lNonEUSimpositionUS#c.zllnGBAidTotPGDPWDIa  = llEUSimpositionUS ///
		l.zlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) ///
		i.ccode2 i.year902, first vce(cluster ccode2) 	
		
			
**# Plot Figure 1a
	* Re-estimate 2sls model from columns 5, 6, and 7
		ivregress 2sls absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl  ///
		(lEUSimpositionUS c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa = ///
		llEUSimpositionUS lzlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) /// 
		i.ccode2 i.year902, first vce(cluster ccode2) 
	* margins
		margins, at(zllnGBAidTotPGDPWDIa=(-1.5(0.05)2)) dydx(lEUSimpositionUS) vsquish	
	* plot
		marginsplot, addplot(hist zllnGBAidTotPGDPWDIa if zllnGBAidTotPGDPWDIa<2 & zllnGBAidTotPGDPWDIa>-1.5, ///
		yaxis(2) yscale(alt)  xlabel(-1.5 "-1.5 Std.Dev." -1 "-1 Std.Dev." -.5 "-.5 Std.Dev." ///
		0 "Mean" 0.5 "+.5 Std.Dev." 1 "+1 Std.Dev." 1.5 "+1.5 Std.Dev." 2 "+2 Std.Dev.", ///
		labsize(small) alt) ylabel(-1(.5)1.5) color(none) blcolor(black%20)) ///
		level(95) recast(line) plot1opts(lcol(red)) legend(off) ciopt(color(red%30)) recastci(rarea) yline(0)  ///
		xtitle(Log(U.S. Aid as % of Recipient GDP), size(small)) ///
		ytitle("Marginal Effect of Sanctions", size(small)) ///
		title("Average Marginal Effect of Sanctions on UNGA Vote Dissimilarity", size(small)) 
		gr_edit .yaxis2.title.style.editstyle size(small) editcopy
		gr play "Fig1A.grec"

**# Plot Figure 1b: (marginal effect of aid during sanctions and no sanctions)
	*Re-estimate the model
		ivregress 2sls absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl  ///
		(lEUSimpositionUS c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa = llEUSimpositionUS ///
		lzlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) /// 
		i.ccode2 i.year902, first vce(cluster ccode2) 
	* margins
		margins, at(lEUSimpositionUS=(0 1)) dydx(zllnGBAidTotPGDPWDIa) vsquish
	* plot
		marginsplot, level(95) yline(0) ylabel( -.35(.05).15) xlabel(0 "No Sanction" 1 "Sanction") ///
		recast(scatter) xtitle("") ///
		ytitle("Marginal Effect of U.S. Aid") ///
		title("Average Marginal Effect of U.S. Aid on UNGA Vote Dissimilarity") 
		gr play "Fig1B.grec"	

**# Plot Figure 2a: Predicted outcomes
	* Re-estimate the model
		ivregress 2sls absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl  ///
		(lEUSimpositionUS c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa = llEUSimpositionUS ///
		lzlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) /// 
		i.ccode2 i.year902, first vce(cluster ccode2) 
	* margins
		margins, at(zllnGBAidTotPGDPWDIa=(-1.5(0.05)2) lEUSimpositionUS=(1)) vsquish
	* plot
		marginsplot, addplot(hist zllnGBAidTotPGDPWDIa if zllnGBAidTotPGDPWDIa<2 & zllnGBAidTotPGDPWDIa>-1.5, ///
		yaxis(2) yscale(alt) ///
		xlabel(-1.5 "-1.5 Std.Dev." -1 "-1 Std.Dev." -.5 "-.5 Std.Dev." ///
		0 "Mean" 0.5 "+.5 Std.Dev." 1 "+1 Std.Dev." 1.5 "+1.5 Std.Dev." 2 "+2 Std.Dev.", ///
		labsize(small) alt) /// 
		ylabel(2(.5)5) color(green%10)) ///
		level(95) recast(line) legend(off)  ciopt(color(%30)) recastci(rarea) yline(0)  ///
		xtitle(U.S. Aid as % of Recipient GDP) ///
		ytitle("Linear Prediction") 
		gr_edit .yaxis2.title.style.editstyle size(small) editcopy
		gr play "Fig2A.grec"

**# Plot Figure 2b: Predicted outcomes
	* Re-estimate the model
		ivregress 2sls absidealimportantdiff zllnGBAidTotPGDPWDIa lEUSThOnly $cvarl  ///
		(lEUSimpositionUS c.lEUSimpositionUS#c.zllnGBAidTotPGDPWDIa = llEUSimpositionUS ///
		lzlImpUSXllnAidPGDPa lEUSUSTot zlEUSUSTotXllnAidPGDPa) /// 
		i.ccode2 i.year902, first vce(cluster ccode2) 
	* margins
		margins, at(zllnGBAidTotPGDPWDIa=(-1.5(0.05)2) lEUSimpositionUS=(0)) vsquish
	* plot
		marginsplot, addplot(hist zllnGBAidTotPGDPWDIa if zllnGBAidTotPGDPWDIa<2 & zllnGBAidTotPGDPWDIa>-1.5, ///
		yaxis(2) yscale(alt) ///
		xlabel(-1.5 "-1.5 Std.Dev." -1 "-1 Std.Dev." -.5 "-.5 Std.Dev." ///
		0 "Mean" 0.5 "+.5 Std.Dev." 1 "+1 Std.Dev." 1.5 "+1.5 Std.Dev." 2 "+2 Std.Dev.", ///
		labsize(small) alt) /// 
		ylabel(2(.5)5) color(green%10)) ///
		level(95) recast(line) legend(off) ciopt(color(eltblue%30)) recastci(rarea) yline(0)  ///
		xtitle(U.S. Aid as % of Recipient GDP) ///
		ytitle("Linear Prediction") 
		gr_edit .yaxis2.title.style.editstyle size(small) editcopy
		gr play "Fig2B.grec"
	
exit
















	

	